//
// Created by ZLM on 2016/7/26.
//
#include "com_example_zlm_ndk_NDKUtils.h"
#include "aes.h"
#include "string.h"

const char keyValue[] = {
        21, 25, 21, -45, 25, 98, -55, -45, 10, 35, -45, 35,
        26, -5, 25, -65, -78, -99, 85, 45, -5, 10, -0, 11,
        -35, -48, -98, 65, -32, 14, -67, 25, 36, -56, -45, -5,
        12, 15, 35, -15, 25, -14, 62, -25, 33, -45, 55, 12, -8,
};

const char iv[] =  {    //16 bit
        -33, 32, -25, 25, 35, -27, 55, -12, -15,32,
        23, 45, -26, 32, 5,16
};


char* Jstring2CStr(JNIEnv*env,jstring jstr)
{
char* rtn= NULL;
jclass clsstring=(*env)->FindClass(env,"java/lang/String");

jstring strencode=(*env)->NewStringUTF(env,"UTF-8");

jmethodID mid=(*env)->GetMethodID(env,clsstring,"getBytes","(Ljava/lang/String;)[B");
// String.getByte("GB2312");  

jbyteArray barr=(jbyteArray)(*env)->CallObjectMethod(env,jstr,mid,strencode);

jsize alen= (*env)->GetArrayLength(env,barr);
jbyte* ba= (*env)->GetByteArrayElements(env,barr,JNI_FALSE);

if(alen>0)
{

rtn=(char*)malloc(alen+1);//"\0"  


memcpy(rtn,ba,alen);
rtn[alen]=0;

}

(*env)->ReleaseByteArrayElements(env,barr,ba,0);//  

return rtn;

}

JNIEXPORT jbyteArray JNICALL Java_com_example_zlm_ndk_NDKUtils_getKeyValue
        (JNIEnv *env, jobject obj){
    jbyteArray kvArray = (*env)->NewByteArray(env, sizeof(keyValue));
    jbyte *bytes = (*env)->GetByteArrayElements(env,kvArray,0);

    int i;
    for (i = 0; i < sizeof(keyValue);i++){
        bytes[i] = (jbyte)keyValue[i];
    }

    (*env)->SetByteArrayRegion(env,kvArray, 0, sizeof(keyValue),bytes);
    (*env)->ReleaseByteArrayElements(env,kvArray,bytes,0);

    return kvArray;
}

JNIEXPORT jbyteArray JNICALL Java_com_example_zlm_ndk_NDKUtils_getIv
        (JNIEnv *env, jobject obj){
    jbyteArray ivArray = (*env)->NewByteArray(env, sizeof(iv));
    jbyte *bytes = (*env)->GetByteArrayElements(env,ivArray, 0);

    int i;
    for (i = 0; i < sizeof(iv); i++){
        bytes[i] = (jbyte)iv[i];
    }

    (*env)->SetByteArrayRegion(env,ivArray, 0, sizeof(iv), bytes);
    (*env)->ReleaseByteArrayElements(env,ivArray,bytes,0);

    return ivArray;
}

JNIEXPORT jstring JNICALL Java_com_example_zlm_ndk_NDKUtils_getTime
        (JNIEnv *env, jclass cla, jstring jstr) {


    return (*env)->NewStringUTF(env,"Android Studio NDK JNI developer!");
}




JNIEXPORT jint JNICALL Java_com_example_zlm_ndk_NDKUtils_getIntNumber
        (JNIEnv *env, jclass cla, jstring jstr) {
    int i = 5;
    return (jint)i;
}
